package controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import mensch.Field;
import mensch.Game;
import mensch.Player;
import org.apache.log4j.Logger;

@ManagedBean(name = "gameBean")
@SessionScoped
public class MenschBean implements Serializable {

    private final static Logger LOG = Logger.getLogger(MenschBean.class.getName());
    private Game game;
    private Player player;
    @ManagedProperty(value = "#{loginController}")
    private Login loginController;

    public MenschBean() {
        LOG.debug("Leerer MenschBean Konstruktor // LoginController: " + loginController);

    }

    @PostConstruct
    public void init() {
        LOG.debug("init:: // LoginController: " + loginController);
        player = new Player(loginController.getUsername(), 0);
        game = new Game(player);


    }

    public List<Field> getBoardFields() {

        List<Field> tagList = new ArrayList<Field>();

        for (int i = 0; i < 40; i++) {
//            LOG.debug("Hole Feld "+i+" aus dem Table // FieldIndex: " + game.getTable().getFields().get(i).getFieldIndex());
            tagList.add(game.getTable().getFields().get(i));
        }

        LOG.debug("Size der Startfield Map " + tagList.size());
        return tagList;
    }

    public List<Field> getStartFields(int playerIndex) {
        List<Field> tagList = new ArrayList<Field>();

        for (int i = 40 + playerIndex * 4; i < 44 + playerIndex * 4; i++) {
            tagList.add(game.getTable().getFields().get(i));
        }

        return tagList;
    }

    public List<Field> getFinishFields(int playerIndex) {
        List<Field> tagList = new ArrayList<Field>();

        for (int i = 56 + playerIndex * 4; i < 60 + playerIndex * 4; i++) {
            tagList.add(game.getTable().getFields().get(i));
        }

        return tagList;
    }

    public Game getGame() {
        return game;
    }

    public Login getLoginController() {
        return loginController;
    }

    public void setLoginController(Login loginController) {
        this.loginController = loginController;
    }

    public void setGame(Game game) {
        this.game = game;
    }

    public int getNumberOfPlayers() {
        LOG.debug("Number of Player ist: " + game);
        return game.getNumberOfPlayers();
    }

    public List<Player> getPlayers() {
        return game.getPlayers();
    }

    public String getLeaderName() {
        return game.getLeaderName();
    }

    public int getCurrentRound() {
        return game.getCurrentRound();
    }

    public String getPlayerName(int playerID) {
        return game.getPlayers().get(playerID).getUsername();
    }

    public String getPlayerName() {
        return player.getUsername();
    }

    public int getCurrentDiceValue() {
        return game.getPlayerDiceValue();
    }

    public String fieldTitle(int fieldIndex) {
        Field field = game.getTable().getFields().get(fieldIndex);
        String fieldTitle = field.getTitle();

        if (field.getPawnOnField() != null) {
            fieldTitle += ": " + field.getPawnOnField().getPlayer().getUsername();
        }

        return fieldTitle;
    }

    public String fieldImageFilename(int fieldIndex) {
        Field field = game.getTable().getFields().get(fieldIndex);
        String fieldImageFilename = field.getImageFilename();

        if (field.getPawnOnField() != null) {
            fieldImageFilename += "_player" + (field.getPawnOnField().getPlayer().getPlayerID() + 1);
        }

        return fieldImageFilename + ".png";
    }

    public String getRecentDiceValues() {
        return game.getOpponentDiceValues();
    }

    public boolean isInProgress() {
        return game.isInProgress();
    }

    public String getTime() {
        return game.getTime();
    }

    public void startTurn() {
        game.startTurn();
    }

    public void reset() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    }
}
